﻿/*************************************
Function：压缩批处理窗体
 Version：1.0
    Date：2012-04-28
  Author：rjdong
   Email：ooxxab@gmail.com
 WebSite：http://emake.info
*************************************/

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Forms;

using Wind.Compressor.Business;
using Wind.Compressor.Entity;

namespace Wind.Compressor
{
    public partial class FormBatCompress : Form
    {
        string[] allPaths = new string[0];
        string[] allFiles = new string[0];
        public FormBatCompress()
        {
            InitializeComponent();
        }

        private void bt_SourcePath_Click(object sender, EventArgs e)
        {
            try
            {
                if (!string.IsNullOrEmpty(this.txt_SourcePath.Text))
                    this.folderBrowserDialog1.SelectedPath = this.txt_SourcePath.Text;
                if (this.folderBrowserDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    this.txt_SourcePath.Text = this.folderBrowserDialog1.SelectedPath;
                }
            }
            catch (Exception ex) { MessageBox.Show(ex.Message); }
        }

        private void bt_DestPath_Click(object sender, EventArgs e)
        {
            try
            {
                if (this.folderBrowserDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    this.txt_DestPath.Text = this.folderBrowserDialog1.SelectedPath;
                }
            }
            catch (Exception ex) { MessageBox.Show(ex.Message); }
        }

        private void bt_Set_Click(object sender, EventArgs e)
        {
            FormSetting form2 = new FormSetting();
            form2.ShowDialog();
        }

        private void bt_Compress_Click(object sender, EventArgs e)
        {
            try
            {
                int jsCount = 0;
                int cssCount = 0;

                if (string.IsNullOrEmpty(this.txt_SourcePath.Text))
                {
                    MessageBox.Show("请选择源文件目录");
                    return;
                }
                if (string.IsNullOrEmpty(this.txt_DestPath.Text))
                {
                    MessageBox.Show("请选择目标文件目录");
                    return;
                }
                SettingInfo setting = Utils.LoadSetting();

                SearchOption searchOption = SearchOption.TopDirectoryOnly;
                string sourePath = this.txt_SourcePath.Text;
                string destPath = this.txt_DestPath.Text;
                if (this.chb_IsCludeSubDir.Checked)
                    searchOption = SearchOption.AllDirectories;

                //allPaths = Directory.GetDirectories(sourePath, "", searchOption);
                string searchPattern = "*";
                if (cbb_FileType.SelectedIndex == 1)
                    searchPattern = "*.js";
                else if (cbb_FileType.SelectedIndex == 2)
                    searchPattern = "*.css";
                allFiles = Directory.GetFiles(sourePath, searchPattern, searchOption);

                string path = string.Empty;
                string name = string.Empty;
                string ext = string.Empty;
                string content = string.Empty;
                string fileMidPath = string.Empty;

                setting.CssIsAutoSave = true;
                setting.JsIsAutoSave = true;

                if (allFiles.Length > 0)
                {
                    var n = 1;
                    this.toolStripProgressBar1.Maximum = allFiles.Length;
                    this.toolStripProgressBar1.Visible = true;
                    foreach (string file in allFiles)
                    {
                        path = Path.GetFullPath(file);
                        path = Path.GetDirectoryName(file);
                        fileMidPath = path.Replace(sourePath, "");

                        name = Path.GetFileNameWithoutExtension(file);
                        ext = Path.GetExtension(file).ToLower();

                        this.toolStripStatusLabel1.Text = file;
                        this.toolStripProgressBar1.Value = n;
                        n++;
                        Application.DoEvents();

                        if (ext == ".css" || ext == ".js")
                        {
                            if (ext == ".css")
                                cssCount++;
                            if (ext == ".js")
                                jsCount++;
                            content = YuiBusiness.Instance.DoCompressBat(sourePath, fileMidPath, Path.GetFileName(file), setting, destPath);
                        }
                    }
                    //MessageBox.Show("批处理完成！");
                    this.toolStripStatusLabel1.Text = "批处理完成，Js:" + jsCount.ToString() + "个,Css：" + cssCount.ToString() + "个";
                    this.toolStripProgressBar1.Visible = false;
                }
            }
            catch (Exception ex)
            {
                LogHelper.Write("Wind.Compressor.FormBatCompress.bt_Compress_Click error", ex);
                MessageBox.Show(ex.Message);
            }
        }
    }
}
